<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Userapi_service {

// 	$this 只能在你的控制器、模型或视图中直接使用，如果你想在你自己的类中使用 CodeIgniter 类，你可以像下面这样做：

// 首先，将 CodeIgniter 对象赋值给一个变量:

// $CI =& get_instance();
// 一旦你把 CodeIgniter 对象赋值给一个变量之后，你就可以使用这个变量来 代替 $this
// 	$CI =& get_instance();

// $this->CI->load->helper('url');
	// $this->CI->load->library('session');
// $CI->load->library('session');
// $CI->config->item('base_url');
	
	protected $CI;
	// 如果要往这个类库里面传参数，这里就要构造函数 并且接收数组
	public function __construct()
    {	
        $this->CI =& get_instance(); //将 CodeIgniter 对象赋值给一个变量:
        $this->CI->load->model('Model_m');// 通用模型 实例 增删改查 的模型 
		$this->CI->load->model('Users_m');// 用户表 实例 增删改查 的模型
		$this->CI->load->model('Device_m');// 用户表 实例 增删改查 的模型
		$this->CI->load->model('Recharge_m');// 用户表 实例 增删改查 的模型
    }
    
	// public function user_api_get($get)
	// {	
	// 	if(empty($get['device_sn'])){
	// 		$data['error'] = error(4006);
	// 		return $data;
	// 	}
	// 	$device_where = array('device_sn'=>$get['device_sn']); //where
 //    	$data['user'] = $this->CI->Users_m->select($user_where);
 //    	$data['success'] = success(4000);

 //    	return $data;
	// }

	public function device_sn_get($get)
	{	

		$device_where = array('device_sn'=>$get['device_sn']); //where
    	$device = $this->CI->Device_m->select($device_where);
    	if(empty($device[0]['user_id'])){
    		$data['success'] = error(4006); //请求要求成功返回信息
			return $data;
			exit;
    	}
    	
    	$recharge_where = '';
    	$data['device'] = $this->CI->Device_m->select($device_where);
    	$data['recharge'] = $this->CI->Recharge_m->select($recharge_where);
    	return $data;
	}
	
	public function user_binding_device($put)
	{	
		$device_where = array('device_sn'=>$put['device_sn']); //where
		$device = $this->CI->Device_m->select($device_where); //如果用户已存在
		
		if($device[0]['user_id']){
	        $data['success'] = error(5000);
	    	//$data['device'] = $device[0];
	    	return $data;
			exit;
		}else{

			$data_uid = array('user_id' => $put['user_id']); 
			$user_data_attributes = array('user_id' => $put['user_id'],'carid_image_z' => $put['carid_image']['carid_image_z'],'carid_image_f' => $put['carid_image']['carid_image_f'],'carid_image_s' => $put['carid_image']['carid_image_s']);

			$user_data = array('carid' => $put['carid'],'mobile' => $put['mobile'],'username_qq' => $put['username_qq'],'realname' => $put['realname'],'password' => $put['password']);
			//设备绑定用户
			$up_device = $this->CI->Device_m->update($device_where,$data_uid);
			$add_users_attributes = $this->CI->Model_m->insert('users_attributes',$user_data_attributes);
			$up_user = $this->CI->Users_m->update($data_uid,$user_data);
			
			$bangding_device = $this->CI->Device_m->select($device_where); //如果用户已存在 users_attributes
			$user_where = array('user_id'=>$bangding_device[0]['user_id']);
			$user_weixin_where = array('user_id'=>$bangding_device[0]['user_id']);
			$user_attributes_where = array('user_id'=>$bangding_device[0]['user_id']);
			$device_data['device'] = $bangding_device[0];
			$device_data['user'] = $this->CI->Users_m->select($user_where);
			$device_data['weixin'] = $this->CI->Model_m->select('users_weixin',$user_weixin_where);
			$device_data['users_attributes'] = $this->CI->Model_m->select('users_attributes',$user_attributes_where); 
			$device_data['success'] = success(4000);
			//返回新添加的用户的信息
			
	    	return $device_data;
	    	
		}
	}

	public function user_data_verification($post)
	{	
		if(empty($post['device_sn']) || empty($post['carid']) || empty($post['mobile']) || empty($post['realname']) || empty($post['username_qq']))
		{
			$data['success'] = error(4009); //请求要求成功返回信息
			echo _echo($data);
			exit;
		}
	}


	
	// public function user_api_put($put)
	// {	
	// 	$user_where = array('user_id'=>$put['user_id']); //where
	// 	$user = $this->Userapi_m->select($user_where);

	// 	if(empty($put['user_id'])){
	// 		$data['error'] = error(4006);
	//     	return $data['error'];
	// 	}else{
			
	// 		//修改用户
	// 		$data'username' = $put['username'];
	// 		$data'password' = $put['password'];
	// 		$data'mobile' = $put['mobile'];
	// 		$data'email' = $put['email'];

	// 		$user_update = $this->Userapi_m->update($user_where,$data);
				
	//         $user_weixin_where = array('user_id'=>$put['user_id']); //where
	//         $user_data['success'] = success(4000);
	//     	$user_data['user'] = $this->Userapi_m->select($user_where);
	//     	$user_data['weixin'] = $this->AppAuth_m->select($user_weixin_where);
	    				
	//     	if($user_data['user'][0]['user_id']){
	//     		return $user_data;
	//     	}
	// 	}	
		
	// }
	
	
	// public function user_api_delete($delete)
	// {	
	// 	$user_where = array('user_id'=>$delete['user_id']); //where
	// 	$user = $this->Userapi_m->select($user_where);

	// 	if(empty($delete['user_id'])){
	// 		$data['error'] = error(4006);
	//     	return $data['error'];
	// 	}else{
	// 		//删除用户
	// 		$user_delete = $this->Userapi_m->delete($user_where);
	//         $data['success'] = success(4000);
	//     	return $data;
	// 	}
	// }
}